package view;

import Dao.Blog;
import Dao.BlogDao;
import Dao.User;
import common.Util;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/personlist.html")
public class personServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");

        User user = Util.checkLoginStatus(req);
        if(user == null){
            System.out.println("当前未登录");
            resp.sendRedirect("login.html");
            return;
        }

        // 1. 从数据库中拿到自己写过的博客
        BlogDao blogDao = new BlogDao();
        List<Blog> list = blogDao.selectAllPerson(user.getUserId());

        // 2. 渲染
        TemplateEngine engine = (TemplateEngine) getServletContext().getAttribute("engine");
        WebContext webContext = new WebContext(req,resp,getServletContext());
        webContext.setVariable("blogs",list);
        webContext.setVariable("num",blogDao.selectTotal(user.getUserId()));
        webContext.setVariable("user",user);
        engine.process("personlist",webContext, resp.getWriter());
    }
}
